﻿package server;

import java.sql.ResultSet;
import java.sql.SQLException;

public class BankAccList {
	private Data data;
	
	public BankAccList(){
		data=new Data();
	}
	
	public boolean searchID(int pID){
		//妫€绱D鏄惁瀛樺湪锛屾槸鍒欒繑鍥瀟rue
		String sql;
		ResultSet rs=null;
		
		sql="select * from bankacclist where pID=" + pID;
		rs=data.getResultSet(sql);
		try {
			if(rs.next()){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}
	
	public int addAcc(int pID,String pname,int pkey){
		
		//杩斿洖涓€涓郴缁熺敓鎴愮殑閾惰璐﹀彿锛屽嵆CANum
		//鑻ユID宸叉敞鍐岃繃璐︽埛锛屽垯杩斿洖-1
		String sql;
		String sql2;
		int re=0;
		ResultSet rs=null;
		int caNum=-1;
		
		if(searchID(pID)){
			return -1;
		}
		
		sql="insert into bankacclist values (null,'"+pname+"',"+pkey+",0,"+pID+")";
		sql2="select * from bankacclist order by id desc limit 1";
		re=data.update(sql);
		
		if(re==1){
			rs=data.getResultSet(sql2);
			try {
				if(rs.next()){
					caNum=rs.getInt("id");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return caNum;
		//若此ID已注册过账户，则返回-1
	}
	
	public int getKey(int pID){
		//鍙傛暟ID,杩斿洖瀵嗙爜key
		
		String sql;
		ResultSet rs=null;
		int key=0;
			
		sql="select * from bankacclist where pID="+pID;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				key=rs.getInt("bkey");
		}
			} catch (SQLException e) {
			
			e.printStackTrace();
			}		
		return key;
	}
	
	public int getCANum(int pID){
		//鍙傛暟ID锛岃繑鍥炶处鍙稢ANum
		String sql;
		ResultSet rs=null;
		int caNum=0;
			
		sql="select * from bankacclist where pID="+pID;
		rs=data.getResultSet(sql);
		try {
			if(rs.next()){
				caNum=rs.getInt("id");
		}
			} catch (SQLException e) {
			
			e.printStackTrace();
			}		
		return caNum;
	}
	
	public double getCABalance(int CANum){
		//鍙傛暟璐﹀彿CANum锛岃繑鍥炶处鎴蜂綑棰滳ABalance
		String sql;
		ResultSet rs=null;
		double balance=0;
			
		sql="select * from bankacclist where id="+CANum;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				balance=rs.getDouble("caBalance");
		}
			} catch (SQLException e) {
			
			e.printStackTrace();
			}		
		return balance;
	}
	
	public boolean setCABalance(int CANum,double CABalance){
		//鎶婅处鍙蜂负CANum鐨勮处鎴蜂綑棰滳ABalance鐨勫€煎彉涓哄弬鏁癈ABalance鐨勫€?
		String sql;
		int re=0;

		sql="update bankacclist set caBalance="+CABalance+" where id="+CANum;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;
	}
	
	public boolean setKey(int pID,int pkey){
		//将ID为pID的表项密码改为pkey
		String sql;
		int re=0;

		sql="update bankacclist set bkey="+pkey+" where pID="+pID;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;
	}
	
	public int getSize(){
		String sql;
		ResultSet rs=null;
		int sum=0;
			
		sql="select count(*) as sum from bankacclist";
		rs=data.getResultSet(sql);
		try {
			if(rs.next()){
				sum=rs.getInt("sum");
		}
			} catch (SQLException e) {
			
			e.printStackTrace();
			}		
		return sum;
	}

}
